package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.zltx.bean.*;
import com.jy.adscm.zltx.mapper.mysql.*;
import com.jy.adscm.zltx.service.QsFileContentService;
import com.jy.adscm.zltx.service.QsFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 质量体系文件 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-10-15
 */
@Service
public class QsFileServiceImpl extends ServiceImpl<QsFileMapper, QsFile> implements QsFileService {

    Logger logger = LoggerFactory.getLogger(getClass());

     @Autowired
      QsFileMapper qsFileMapper;
    @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    JyZltxListTreeMapper JyZltxListTreeMapper;
     @Autowired
    QsFileContentMapper qsFileContentMapper;
     @Autowired
    QsApplyUpdateRecordMapper qsApplyUpdateRecordMapper;
     @Autowired
    QsFileVersionRecordMapper qsFileVersionRecordMapper;

    @Override
    public  QsFile selectByFileId(Integer fileId){
        QsFile qsFile = super.selectById(fileId);
      if (qsFile!=null){
          if (qsFile.getFileApprover()!=null&&!"".equals(qsFile.getFileApprover())){
              JyUser fileApprover = jyUserMapper.selectById(qsFile.getFileApprover());
              qsFile.setFileApproverName(fileApprover.getName());
          }
          if (qsFile.getFileWriter()!=null&&!"".equals(qsFile.getFileWriter())){
              JyUser fileWriter = jyUserMapper.selectById(qsFile.getFileWriter());
              qsFile.setFileWriterName(fileWriter.getName());
          }
      }
        return  qsFile;
     }
     @Transactional
     @Override
    public boolean insertQsFile(QsFile qsFile, String treeId) throws AdscmSystemException {
        boolean insert = super.insert(qsFile);
        JyZltxListTree jyZltxListTree = JyZltxListTreeMapper.selectById(treeId);
          if (jyZltxListTree!=null){
              jyZltxListTree.setLinkFileId(qsFile.getFileId());
              JyZltxListTreeMapper.updateById(jyZltxListTree);
          }else {
          throw     new AdscmSystemException("拿到的treeId没找到对应的树");
          }
        return insert;
    }
    @Transactional
    @Override
    public boolean updateQsFile(QsFile qsFile) throws  AdscmSystemException{
        //说明是第一次发布
        boolean b=false;
        //版本发布
        if ( "1".equals(qsFile.getIsApproval()) ){
            if (qsFile.getUpdateTimes()==null){
                qsFile.setUpdateTimes(0);

            }else {
                qsFile.setUpdateTimes(qsFile.getUpdateTimes()+1);
            }
            qsFile.setLastUpdateTime(new Date());
            b = super.updateById(qsFile);
            //查询到当前所有filecontent内容 修改版本号
            Wrapper<QsFileContent> wrapper=new EntityWrapper<>();
            wrapper.eq("fileId",qsFile.getFileId());
            wrapper.eq("isUse","1");
            List<QsFileContent> qsFileContents = qsFileContentMapper.selectList(wrapper);
            if (qsFileContents.size()>0){
                for (QsFileContent qsFileContent : qsFileContents) {
                    qsFileContent.setIsLocked(1);
                    qsFileContentMapper.updateById(qsFileContent);
                    QsFileVersionRecord  qsFileVersionRecord=new QsFileVersionRecord();
                    qsFileVersionRecord.setFileCode(qsFile.getFileCode());
                    qsFileVersionRecord.setFileContentId(qsFileContent.getFileContentId());
                    qsFileVersionRecord.setFileId(qsFile.getFileId());
                    qsFileVersionRecord.setVersionCode(qsFile.getVersionCode());
                    qsFileVersionRecord.setIssuer(qsFile.getFileApprover());
                    qsFileVersionRecord.setIssueTime(new Date());
                    qsFileVersionRecordMapper.insert(qsFileVersionRecord);
                }
            }
             if (qsFile.getUpdateTimes()>0){
                 //把申请表单里面的完成标志变成1
                 Wrapper<QsApplyUpdateRecord> wrapper1=new EntityWrapper<>();
                 wrapper1.eq("fileId",qsFile.getFileId());
                 wrapper1.eq("isFinishUpdate","0");
                 List<QsApplyUpdateRecord> qsApplyUpdateRecords = qsApplyUpdateRecordMapper.selectList(wrapper1);
                 if (qsApplyUpdateRecords.size()>0){
                     QsApplyUpdateRecord qsApplyUpdateRecord = qsApplyUpdateRecords.get(0);
                     qsApplyUpdateRecord.setIsFinishUpdate("1");
                     qsApplyUpdateRecordMapper.updateById(qsApplyUpdateRecord);
                 }else {
                     throw   new  AdscmSystemException("没找到申请记录");
                 }
             }

        }else {
         b=   super.updateById(qsFile);
        }
        return b;
}

    @Override
    public NewAddQsFile printCurrentVersion(Integer fileId) throws AdscmSystemException {
        NewAddQsFile  newAddQsFile= new NewAddQsFile();
        QsFile qsFile = qsFileMapper.selectById(fileId);
        Wrapper<QsFileContent> wrapper=new EntityWrapper<>();
        wrapper.eq("fileId",fileId);
        wrapper.eq("isUse","1");
        wrapper.orderBy("contentType");
        List<QsFileContent> qsFileContents = qsFileContentMapper.selectList(wrapper);
        newAddQsFile.setQsFile(qsFile);
        newAddQsFile.setQsFileContents(qsFileContents);
        return newAddQsFile;
    }

    @Override
    public List<QsFileVersion> historyVersion(Integer fileId) {
        //根据fileid查询出对应的版本号有哪些;
        List<QsFileVersion> qsFileVersions =new ArrayList<>();
        Wrapper< QsFileVersionRecord> wrapper =new EntityWrapper<>();
        wrapper.eq("fileId",fileId);
        wrapper.groupBy("versionCode");
        List<QsFileVersionRecord> qsFileVersionRecords = qsFileVersionRecordMapper.selectList(wrapper);
        if (qsFileVersionRecords.size()>0){
            for (QsFileVersionRecord qsFileVersionRecord : qsFileVersionRecords) {
                QsFileVersion  qsFileVersion =new QsFileVersion();
                qsFileVersion.setFileId(qsFileVersionRecord.getFileId());
                qsFileVersion.setFileCode(qsFileVersionRecord.getFileCode());
                qsFileVersion.setVersionCode(qsFileVersionRecord.getVersionCode());
                if (qsFileVersionRecord.getIssuer()!=null&&!"".equals(qsFileVersionRecord.getIssuer())){
                    JyUser jyUser = jyUserMapper.selectById(qsFileVersionRecord.getIssuer());
                    qsFileVersion.setIssuer(jyUser.getName());
                }
                qsFileVersion.setIssueTime(qsFileVersionRecord.getIssueTime());
                qsFileVersion.setVersionDesc(qsFileVersionRecord.getVersionDesc());
                List<Integer>   condition=new ArrayList<>();
                Wrapper< QsFileVersionRecord> wrapper1 =new EntityWrapper<>();
                wrapper1.eq("fileId",fileId);
                wrapper1.eq("versionCode",qsFileVersionRecord.getVersionCode());
                List<QsFileVersionRecord> qsFileVersionRecords1 = qsFileVersionRecordMapper.selectList(wrapper1);
                if (qsFileVersionRecords1.size()>0){
                    for (QsFileVersionRecord fileVersionRecord : qsFileVersionRecords1) {
                        condition.add( fileVersionRecord.getFileContentId());
                    }
                    List<QsFileContent> qsFileContents= qsFileContentMapper.selectByCondition(condition);
                    qsFileVersion.setQsFileContents(qsFileContents);
                }
                qsFileVersions.add(qsFileVersion);
            }
        }
        return qsFileVersions;
    }

    @Override
    public boolean updateQsFileVersion(QsFileVersion qsFileVersion) {
        Wrapper< QsFileVersionRecord> wrapper =new EntityWrapper<>();
        wrapper.eq("fileId",qsFileVersion.getFileId());
        wrapper.eq("versionCode",qsFileVersion.getVersionCode());
        List<QsFileVersionRecord> qsFileVersionRecords = qsFileVersionRecordMapper.selectList(wrapper);
        for (QsFileVersionRecord qsFileVersionRecord : qsFileVersionRecords) {
            qsFileVersionRecord.setVersionDesc(qsFileVersion.getVersionDesc());
            qsFileVersionRecordMapper.updateById(qsFileVersionRecord);
        }
        return true;
    }

}
